//sessionChoose 1是带sessionID的GET方法  2是不带sessionID的GET方法, 3是带sessionID的Post方法,4是不带sessionID的Post方法
//ask是是否要进行询问授权，true为要，false为不要
//sessionChoose为1,2,3,4,所以paramSession下标为0的则为空

// const baseUrl = "https://m4.xiaoyu.com/index.php/jjd/";

import API from './config.js'
function HttpRequst(loading, url, sessionChoose, sessionId, params, method, ask, callBack) {
  if (loading == true) {
    wx.showToast({
      icon: 'loading'
    })
  }
  var params = Object.prototype.toString.call(params) == "[object Object]" ? params :{};
  params["ukey"] = sessionId;
  var paramSession = sessionChoose == 1 ? { 'content-type': 'application/x-www-form-urlencoded' } : { 'content-type': 'application/json' };
  
  wx.request({
    url: API.baseUrl + url,
    data: params,
    dataType: "json",
    header: paramSession,
    method: method,
    success: function (res) {
      
      if (loading == true) {
        wx.hideToast();//隐藏提示框
      }
      if(res.data.status==403){
        wx.showModal({
          title: '提示',
          content: '您未登录，请先登录',
          cancelText: '我知道了',
          confirmText: '立即登录',
          success: function (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/index/login/login',
              })
            } else if (res.cancel) {

            }
          }
        })
      }else{
        setTimeout(function () {
          callBack(res.data);
        })
      } 
     
    },
    complete: function () {
      if (loading == true) {
        wx.hideToast();//隐藏提示框
      }
    }
  })
}




module.exports = HttpRequst
